<template>
	<view>
		<view class="cu-card case" :class="isCard?'no-card':''" v-for="item in activityList" :key="item.id">
			<view class="cu-item shadow">
				<view class="image" style="height: 300px;" @tap="todetail(item.id,item.userId)">
					<image :src="item.ai[0].url"
					 mode="aspectFill" style="height: 300px;"></image>
					<view class="cu-bar bg-shadeBottom"> <text class="text-cut">{{item.activityDesc}}</text></view>
				</view>
				<view class="cu-list menu-avatar">
					<view class="cu-item" @tap="todetail(item.id,item.userId)">
						<view class="cu-avatar round lg" style="background-image:url(https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png);"></view>
						<view class="content flex-sub">
							<view class="text-grey">{{'名称:'+item.activityName}}</view>
							<view class="text-gray text-sm flex justify-between">
								{{item.activityDate}}
								<view class="text-gray text-sm">
									参与情况：{{item.tookNumber + '/' + item.limitNumber}}
								</view>
							</view>
						</view>
					</view>
					<view style="display: flex;flex-direction: row;">
						<button class="btn" style="width: 50%;" plain='true' @click="getid(item.id)" data-target="DialogModal2">删除</button>
						<button class="btn" style="width: 50%; color: #0081FF" plain='true' @tap="topartner(item.activityName)">管理</button>
					</view>
				</view>
			</view>
		</view>
		<view v-if="this.activityList.length == 0" style="width: 100%; padding: 20px 0 0 0;text-align: center;">
			<text style="font-size: 18px;">暂时还没有发布过活动哟</text>
		</view>
		
		<view class="cu-modal" :class="modalName=='DialogModal2'?'show':''">
			<view class="cu-dialog">
				<view class="cu-bar bg-white justify-end">
					<view class="content">提醒</view>
					<view class="action" @tap="hideModal">
						<text class="cuIcon-close text-red"></text>
					</view>
				</view>
				<view class="padding-xl">
					亲，确定要删除吗？
				</view>
				<view class="cu-bar bg-white">
					<view class="action margin-0 flex-sub text-green " @tap="hideModal">
						<view class="action margin-0 flex-sub text-green solid-right" @tap="hideModal">取消</view>
					</view>
					<view class="action margin-0 flex-sub  solid-left" style="color: red;" @tap="cardelete">确定</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				id: '',
				activityList:[],
				isCard: false,
				modalName: null,
				activityId:''
			}
		},
		onLoad(id) {
			this.id = id.id
			uni.startPullDownRefresh();
			this.getlist()
		},
		onPullDownRefresh() {
			this.getlist()
		    setTimeout(function () {
		        uni.stopPullDownRefresh();
		    }, 1000);
		},
		methods: {
			topartner(name) {
				uni.navigateTo({
				    url: './partner?activityId='+this.activityid+'&activityName='+name,
				});
			},
			todetail(activityid,userID) {
				let ismine
				if(userID == this.id) {
					ismine = true
					uni.navigateTo({
						url: '../../activitydeatil/activitydeatil?activityid='+activityid+'&ismine='+ismine,
					    success: res => {}
					});
				}else{
					ismine = false
					uni.navigateTo({
						url: '../../activitydeatil/activitydeatil?activityid='+activityid+'&ismine='+ismine,
					    success: res => {}
					});
				}
			},
			cardelete() {
				let that = this
				uni.request({
					url: 'http://101.37.15.60:8080/activity/delete',
					// url: 'http://localhost:8080/activity/delete',
					method: 'DELETE',
					header: {
						'Content-Type': 'application/x-www-form-urlencoded'
					},
					data: {
						id: that.activityId
					},
					success: (res) => {
					    if(res.data.message == 'SUCCESS') {
							this.modalName = null
							uni.showToast({
							    title: '删除成功。',
							    duration: 500
							})
							// setTimeout(function(){ 
							// 	uni.navigateTo({
							// 	    url: '../components/lunched'
							// 	});
							// }, 1000);
						}
					}
				})

			},
			getid(id) {
				this.activityId = id
				this.modalName = "DialogModal2"
			},
			// showModal(e) {
			// 	this.modalName = e.currentTarget.dataset.target
			// },
			hideModal(e) {
				this.modalName = null
			},
			getlist() {
				let that= this
				let id = that.id
				uni.request({
					url: 'http://101.37.15.60:8080/activity/selectByUserId',
					data: {
						userId: id
					},
					success: (res) => {
					    if(res.data.message == 'SUCCESS') {
							that.activityList = res.data.data
							for(let i = 0;i<res.data.data.length;i++){
							  let time = res.data.data[i].activityDate
							  let arrTime = []
							  arrTime = time.split(' ')
							  let newTime = arrTime[0]
							  that.activityList[i].activityDate = newTime
							}
						}
					}
				})
			}
		}
	}
</script>

<style scoped>
	.btn {
		border: #EEEEEE 1px solid;
		color: rgb(239, 109, 89);;
	}
</style>
